package com.p.lib.widget.dialog;

import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.heartware.sh.commonlib.R;


public class PromptDialogFragment extends DialogFragment {

	private int width;
	
	private int height;
	
	private Button confirm;
	
	private TextView title;
	
	private TextView content;

	private String titleText;
	
	private String contentText;
	
	private String buttonText;
	
	private int contentColor;
	
	private static onDialogButtonClickListener buttonOnClickListener;
	
	private boolean isUpdate = false;

	public static PromptDialogFragment newInstance(String title, String content, String buttonText) {
		PromptDialogFragment dialogFrag = new PromptDialogFragment();
		Bundle args = new Bundle();
		args.putString("titleText", title);
		args.putString("contentText", content);
		args.putString("buttonText", buttonText);
		dialogFrag.setArguments(args);
		
		return dialogFrag;
	}
	
	public static PromptDialogFragment newInstance(String title, String content, String buttonText, int contentColor) {
		PromptDialogFragment dialogFrag = new PromptDialogFragment();
		Bundle args = new Bundle();
		args.putString("titleText", title);
		args.putString("contentText", content);
		args.putString("buttonText", buttonText);
		args.putInt("contentColor", contentColor);
		dialogFrag.setArguments(args);
		
		return dialogFrag;
	}

	public static interface onDialogButtonClickListener {
		public void confrimButtonClcik();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		titleText = getArguments().getString("titleText");
		
		contentText = getArguments().getString("contentText");
		
		buttonText = getArguments().getString("buttonText");
		
		contentColor = getArguments().getInt("contentColor");
		
		int style = DialogFragment.STYLE_NORMAL, theme = 0;
		setStyle(style,theme);
	}

	@Override
	public void onStart() {
		super.onStart();
		if (getDialog() == null) {
			return;
		}
		setDialogSize();
		getDialog().getWindow().setWindowAnimations(R.style.DialogAnimation);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		
		getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		
		getDialog().setCanceledOnTouchOutside(false);	//点击边缘不消失...
		
		
		
		View v = inflater.inflate(R.layout.view_prompt_dialog, container, false);
		title = (TextView) v.findViewById(R.id.tv_title);
		content = (TextView) v.findViewById(R.id.tv_content);
		confirm = (Button) v.findViewById(R.id.btn_confrim);
		
		title.setText(titleText);
		content.setText(contentText);
		if(contentColor != 0){
			content.setTextColor(getResources().getColor(contentColor));
		}
		
		if(!TextUtils.isEmpty(buttonText)){
			confirm.setText(buttonText);
		}
	
		if(!TextUtils.isEmpty(contentText)){
			content.setVisibility(View.VISIBLE);
		}
		
//		if(confirm.getText().toString().equals(getResources().getString(R.string.txt_update))){
//			isUpdate = true;
//
//			getDialog().setOnKeyListener(new OnKeyListener() {
//
//				@Override
//				public boolean onKey(DialogInterface arg0, int arg1, KeyEvent event) {
//					// TODO Auto-generated method stub
//
//					if(arg1 == KeyEvent.KEYCODE_BACK){
//						return true;
//					}else{
//						return false;
//					}
//
//				}
//			});
//		}else{
//			isUpdate = false;
//			getDialog().setOnKeyListener(null);
//		}
		
		confirm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(null != buttonOnClickListener){
					buttonOnClickListener.confrimButtonClcik();
				}
				getDialog().dismiss();
			}
		});
		return v;
	}
	
	public void setOnButtonClickListener(onDialogButtonClickListener listener){
		buttonOnClickListener = listener;
	}
	
	private void setDialogSize(){
		Window dialogWindow = getDialog().getWindow();
		dialogWindow.setGravity(Gravity.CENTER);
		
		WindowManager w = getActivity().getWindowManager();
		DisplayMetrics dm= new DisplayMetrics();
		w.getDefaultDisplay().getMetrics(dm);
		
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		if(dm.widthPixels < dm.heightPixels){
			width = (int) (dm.widthPixels * 0.75);
			height = (int) (dm.heightPixels * 0.25);
		}
		lp.width = width;
		lp.height = height;
		lp.alpha = 1f;
		dialogWindow.setAttributes(lp);
	}
	
	@Override
	public void onDismiss(DialogInterface dialog) {
		// TODO Auto-generated method stub
		if(isUpdate){
			return;
		}else{
			
			super.onDismiss(dialog);
		}
	}
	
	@Override
	public void onCancel(DialogInterface dialog) {
		// TODO Auto-generated method stub
		if(isUpdate){
			return;
		}else{
			super.onCancel(dialog);
		}
	}
	
}
